
<template>
  <div>
  <biaods ref="biaods"></biaods>
    <vol-box
      :lazy="true"
      v-model="dialogVisible"
      title="样板BOM"
      :height="500"
      :width="1300"
      :padding="1"
      :draggable="true">
      <div>
        
<div class="role-container">
    <div class="role-tree-left flex-col">
      <div class="title"><i class="el-icon-setting"></i>{{this.$store.getters.data().SHUOM}}</div>
       <el-scrollbar class="el-role-list">
       
        <el-tree
           class="tree-line"
          :indent="0"
            ref="v_tree"
            :data="tree_data"
            node-key="id"
            :default-expand-all="true"
            @node-expand="node_expand"
            @node-click="handleNodeClick"
          >
            <template #default="{node}">
              <span>
                <i
                  style="font-size: 10px; font-style: normal" class="el-icon-setting"
                  > </i
                >{{ node.label }}
              </span>
            </template>
          </el-tree>
       
      </el-scrollbar>
    </div>
    <div class="drap-line"></div>
    <div class="role-tree-right flex-col">
       <el-scrollbar class="el-role-list">
      <el-tabs ref="el_tabs" type="border-card" class="role-tree-right-tabs" style="height: 100%;" v-model="activeName">
        <el-tab-pane name="wuliao">
          <template #label>
            <span class="wuliao">
              <el-icon><FolderOpened /></el-icon>
              <span>物料信息</span>
            </span>
          </template>
            <div> 
                <el-table
                  :data="tableData"
                  @row-click="rowclick"
                  style="height:35vh">
                  <el-table-column sortable 
                  v-for="(col,index) in tableColumns"
                  :key=index
                  :prop=col.prop
                  :label=col.label>
                  </el-table-column> 
                </el-table>
              <el-footer>
              <div>共{{this.tableData.length}}条数据 <el-button type="default" size="mini" @click="this.export_cvs(this.tableColumns,this.tableData)">导出</el-button></div></el-footer>
              <p></p>
             
              <el-form :inline="true" :model="curr_data" class="demo-form-inline">
                <el-form-item label="物料号">
                  <el-input  placeholder="物料号" v-model="curr_data.wuliao" disabled></el-input>
                </el-form-item>
                <el-form-item label="物料描述">
                  <el-input  placeholder="物料描述" v-model="curr_data.wuliao_shuom" disabled></el-input>
                </el-form-item>
                <el-form-item label="数量">
                  <el-input  placeholder="数量" v-model="curr_data.jingsl" disabled></el-input>
                </el-form-item>
                <el-form-item label="虚件">
                  <el-input  placeholder="虚件" v-model="curr_data.xunj" disabled></el-input>
                </el-form-item>
                <el-form-item label="生效日期">
                  <el-input  placeholder="生效日期" v-model="curr_data.shengxrq" disabled></el-input>
                </el-form-item>
                <el-form-item label="生效截至日期">
                  <el-input  placeholder="生效截至日期" v-model="curr_data.shixrq" disabled></el-input>
                </el-form-item>
                <el-form-item label="工位">
                  <el-input  placeholder="工位" v-model="curr_data.gongw" disabled></el-input>
                </el-form-item>
                <el-form-item label="父件">
                  <el-input  placeholder="父件" v-model="curr_data.zhuwl" disabled></el-input>
                </el-form-item>
              </el-form>
              <el-form :inline="false" :model="curr_data" class="demo-form-inline">
                
                 <el-form-item  label="表达式">
                 
                  <el-input  placeholder="表达式" :rows="2"   v-model="curr_data.biaods" >  <template #append>
            <el-button @click="search_one" type="primary">
                <el-icon style="vertical-align: middle">
                  <Search />
                </el-icon> </el-button ><!-- 自定义按钮 图标 -->
      </template></el-input>
                </el-form-item>
                 <el-form-item label="表达式说明">
                  <el-input  placeholder="表达式说明" :rows="2"  type="textarea" v-model="curr_data.biaodssm" disabled></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="onSubmit">保存</el-button>
                </el-form-item>
              </el-form>
            </div>
              </el-tab-pane>
        <el-tab-pane name="download-chart">
           <template #label>
            <span class="download-chart">
              <el-icon><TrendCharts /></el-icon>
              <span>数据统计</span>
            </span>
          </template>
            <div>
            。。。。。
            </div>
        </el-tab-pane>
      </el-tabs>
      </el-scrollbar>
    </div>
  </div>
      </div>
      <template #footer>

        <div>
<!-- <el-button
            type="primary"
            size="mini"
            v-show="isshow1"
            @click="get_all_req_list"
            >保存</el-button> -->
          <el-button type="default" size="mini" @click="dialogVisible = false"
            >关闭</el-button
          >
   
        </div></template
      >
    </vol-box>
  </div>
</template>
<script>
import VolBox from "@/components/basic/VolBox.vue";
import { ElMessage, ElMessageBox } from 'element-plus';
import abc from "@/uitils/h_common.js";//这是默认的写法
import biaods from "@/extension/mytable/other/biaods_edit.vue"
export default {
components: {VolBox,abc,ElMessage,ElMessageBox,biaods },
data() {
 return {
    dialogVisible: false,
    activeName:"wuliao",//
    tree_data:[],
    tableData:[],
    curr_data:{wuliao:"",wuliao_shuom:""},
    tableColumns:[{prop:"wuliao",
                   label:"物料号",
                   width:"120",},
                   {prop:"wuliao_shuom",
                   label:"描述",
                   width:"120",},
                   {prop:"hangh",
                   label:"行号",
                   width:"120",},
                   {prop:"jingsl",
                   label:"数量",
                   width:"120",},
                   {prop:"xunj",
                   label:"虚件",
                   width:"120",},
                    {prop:"shengxrq",
                   label:"生效日期",
                   width:"120",},
                   {prop:"biaodssm",
                   label:"表达式说明",
                   width:"120",},
                   {prop:"biaods",
                   label:"表达式",
                   width:"120",},
                   {prop:"gongw",
                   label:"工位",
                   width:"120",}, {prop:"zhuwl",
                   label:"父物料号",
                   width:"120",},
                  ]
 };
 },
 setup() {},
 destroyed() {},
 mounted() {
   // this.dragControllerDiv();//拖拽方法，必须在mounted中调用
    /* setTimeout(() => {
                this.$nextTick(() => {
                   this.dragControllerDiv();
                })
            }, 1000) */
},
methods: {
    show_w(){
         this.$nextTick(() => { 
            this.dialogVisible=true;
            setTimeout(() => {   
                this.dragControllerDiv();//等待页面完成形成，否则左右拖动不了
            },1000);
        });
    },
    rowclick(row,col,ev){
        //console.log(row);
        this.curr_data=row;
    },
    load_all_tree_data() {//一次性加载所有树形数据（数据量少时）//对外调用
      var turl = "/api/yg_jsgf_head/get_yangb_bom?touwul="+this.$store.getters.data().yangbwl;
      this.http.post(turl, {}, true).then((res) => {
        console.log(res);
        let bomlist=res.ybbom_list
        this.tree_data.splice(0, this.tree_data.length); 
        let root_data = bomlist.find((t) => {
          return t.parentId == '';
        }); 
        
        let one;
        if(root_data!=undefined){
            one = {
            id: root_data.id,
            label: root_data.label,
            children: this.CreatTreeData(root_data.id, bomlist),
            };
        }
        else{//当没有单独的根数据，需要手工给跟节点的id
            let root_id=this.$store.getters.data().yangbwl
            one = {
                id: root_id,
                label: root_id,
                children: this.CreatTreeData(root_id, bomlist),
            };
        }
        this.tree_data.push(one);
        this.TreeDataToList(this.tree_data);
        for (let index = 0; index < res.all_ybbom_list.length; index++) {
          const element = res.all_ybbom_list[index];
          let wl=res.wu_list.find(f=>{return f.wuliao==element.wuliao});
          element.wuliao_shuom=wl.shuom
        }
        this.tableData=res.all_ybbom_list;
        this.show_w();//当数据全部加载完成后显示弹出界面
      });
      
    },
     TreeDataToList(treeData) {
      //treedata转换为父子list，2022年11月6日 20:17:57 by huangxs
      for (let index = 0; index < treeData.length; index++) {
        const e = treeData[index];
        //console.log(e);
        if (e.children) {
          //循环每个节点，判断每个节点是否有孩子
          for (let ei = 0; ei < e.children.length; ei++) {
            //有孩子，逐个把孩子插入数组
            const ec = e.children[ei];
            let t = {
              id: ec.label,
              parentid: e.label,
            };
          }
          this.TreeDataToList(e.children); //如果有孩子就递归
        }
      }
    },
    CreatTreeData(currid, list) {
      //创建树形结构数据,currid为数的根节点，list为父子二位表
      const rtn = [];
      for (let index = 0; index < list.length; index++) {
        const element = list[index];
        if (element.parentId === currid) {
          //说明有孩子
          const children = this.CreatTreeData(element.id, list);
          if (children.length > 0) {
            element.children = children;
            element.f_id = currid;
          }
          rtn.push(element);
        }
      }
      return rtn;
    },
    search_one(){
      this.$refs.biaods.get_data_for_out(this.curr_data);
    },
    dragControllerDiv(){//用于拖动使用
            var drapLineDom=document.getElementsByClassName('drap-line');
            var leftDom=document.getElementsByClassName('role-tree-left');
            //var rightDom=document.getElementsByClassName('role-tree-right');
           // var containerDom=document.getElementsByClassName('role-container');
            //console.log(drapLineDom.length);
            //鼠标按下事件
            drapLineDom[0].onmousedown=function(e){
            drapLineDom[0].style.background='#818181';
            var startX=e.clientX;
            
            drapLineDom[0].left=drapLineDom[0].offsetLeft;
            //鼠标拖动事件
            document.onmousemove=function(ee){
               
                var endX=ee.clientX;
               // console.log("endX-startX="+(endX-startX));
                var moveLen=drapLineDom[0].left+(endX-startX);
               // var maxWidth=containerDom[0].clientWidth-drapLineDom[0].offsetWidth;//界面最大宽度
               //console.log("moveLen="+moveLen);
                /* if (moveLen<230){//下载导航最小宽度
                moveLen=230;
                return;
                } */
                /* if (moveLen>maxWidth-780){//下载导航最大宽度
                moveLen=maxWidth-780;
                return;
                } */
                drapLineDom[0].style.left=moveLen;
                leftDom[0].style.width=moveLen-10+'px';
                //不修改display属性，会在拖动过程中出现拖拽条消失情况（拖拽条宽度变成0）
                rightDom[0].style.display="display";
                rightDom[0].style.width=(containerDom[0].clientWidth-moveLen-10)+'px';
            };
            //鼠标松开事件
            document.onmouseup=function(e){
                drapLineDom[0].style.background='#f6f6f6';
                document.onmousemove=null;
                document.onmouseup=null;
                drapLineDom[0].releaseCapture && drapLineDom[0].releaseCapture();
               
            };
            drapLineDom[0].setCapture && drapLineDom[0].setCapture();
            return false;
            };
        },
        export_cvs(table_head,table_data) {
            let str="";
            for(var i=0;i<=table_head.length-1;i++) {
              str += table_head[i].label+","
            }
            str += "\n";
            for(var i=0;i<=table_data.length-1;i++) {
                for(var ii=0;ii<=table_head.length-1;ii++) {
                  //console.log(table_data[i][table_head[ii].prop]);
                  //判断undefined，或者这样判断为空的方式，if(table_data[i][table_head[ii].prop]!=undefined)都可以
                  if(typeof(table_data[i][table_head[ii].prop])!="undefined"){
                    if (table_data[i][table_head[ii].prop]!=null){
                      let tt=table_data[i][table_head[ii].prop].toString();
                      tt= tt.replace(/(\r\n|\n|\r)/gm, "");//过滤回车换行，防止导出cvs报错
                      str += tt+","
                    }else{
                      str += ","
                    }

                  }
                
              } 
                str += "\n";
            }
            //console.log(table_data);
            let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
            //console.log(str);
            //通过创建a标签实现
            let link = document.createElement("a");
            link.href = uri;
            //对下载的文件命名
            link.download = "export.csv";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
      
  }
}
};
</script>
<style lang="less" scoped>
.role-container {
  position: relative;
  background: #f6f6f6;
  top:0px;
  width: 100%;
  height: 80vh;
  left:0;
  bottom: 0;
  padding: 10px;
  display: flex;
  .flex-col {
    display: flex;
    flex-direction: column;
  }
  .role-tree-left {
    border: 1px solid #f2f2f2;
    background: #fff;
    width: 35vh;
    
    margin-right: 10px;
    .title {
      i {
        margin-left: 10px;
      }
    }
  }
  .role-tree-right {
    background: #fff;
    border: 1px solid #f2f2f2;
    width: 0;
    flex: 1;
    .title {
      display: flex;
      i {
        margin-left: 10px;
      }
      div {
        flex: 1;
      }
    }
    .action-group {
      display: flex;
      // line-height: 32px;
      justify-content: center;
      align-items: center;
      label {
        float: left;
      }
      .action-text {
        line-height: 33px;
        label {
          margin-right: 5px;
        }
      }
    }
  }
  .title {
    padding: 10px;
    background: rgb(246 250 255);
    font-weight: bold;
    font-size: 14px;
    letter-spacing: 2px;
  }
  .el-role-list {
    flex: 1;
    height: 0;
    overflow-x: hidden;
  }
}
.role-tree-left ::v-deep(.el-tree-node__content) {
  cursor: pointer;
  height: auto;
  padding: 0px;
  margin: 1px 10px;
  font-size: 12px;
  position: relative;
  a{
    position: absolute;
    display: none;
    right:10px;
  }
}
.role-tree-left ::v-deep(.el-tree-node__content:hover) {
  background: #f4f4f4;
  border-radius: 20px;
  a{
    display:inline-block;
    color:aqua;
  }
}
.role-tree-left ::v-deep(.is-current > .el-tree-node__content:first-child) {
  background: #f2f2f2;
  border-radius: 20px;
}
.role-tree-right ::v-deep(.el-tree-node__content) {
  margin-bottom: 5px;
  height: auto;
  width: 55vh;
}
.role-tree-right ::v-deep(.el-checkbox__label) {
  position: relative;
  top: 2px;
}

.role-tree-right-tabs{
  .el-icon{
    vertical-align: middle;
  }
  span{
    vertical-align: middle;
    // margin-left: 4px;
    padding: 4px;
    font-weight: bold;
    font-size: 14px;
    letter-spacing: 2px;
    color:#000;
  }
}

.drap-line{
  cursor:col-resize;
  position: relative;
  border-radius: 2px;
  width:5px;
}
.el-header, .el-footer {
    background-color: #ebe3e3;
    color: #333;
    text-align:right;
    height: inherit;
  }
</style>

// 树形虚线
<style lang="less">
    .tree-line {
        .el-tree-node {
            position: relative;
            padding-left: 5px; // 缩进量
        }
        .el-tree-node__children {
            padding-left: 5px; // 缩进量
        }

        // 竖线
        .el-tree-node::before {
            content: '';
            height: 100%;
            width: 1px;
            position: absolute;
            left:18px;
            top: -20px;
            border-width: 1px;
            border-left: 1px dashed #52627c;
        }
        // 当前层最后一个节点的竖线高度固定
        .el-tree-node:last-child::before {
            height: 30px; // 可以自己调节到合适数值
        }

        // 横线
        .el-tree-node::after {
            content: '';
            width: 24px;
            height: 20px;
            position: absolute;
            left: 18px;
            top: 10px;
            border-width: 1px;
            border-top: 1px dashed #52627c;
        }

        // 去掉最顶层的虚线，放最下面样式才不会被上面的覆盖了
        & > .el-tree-node::after {
            border-top: none;
        }
        & > .el-tree-node::before {
            border-left: none;
        }

        // 展开关闭的icon
        .el-tree-node__expand-icon {
            font-size: 16px;
            // 叶子节点（无子节点）
            &.is-leaf {
                color: transparent;
                // display: none; // 也可以去掉
            }
        }
        .el-tree-node__content{//每个节点的高度
          height: 20px !important;
        }
    }
</style>

